<?php
ob_start();
include("../UserIncludes/DbConfig.php");
$bk=__getPostVal('bk'); // advertise id
$a=__getPostVal('a');   // adgroup ad id
$g=__getPostVal('g');   // adgroup id
$c=__getPostVal('c');   // campaign id
$s=__getPostVal('s');   // adsite id
$sp=$_REQUEST['val'];   // adspace id md5
$N=$_REQUEST['N'];   // For the campaign ads, type of the target network (Direct or Custom)

$SQL="SELECT adspaceId FROM `adspace_details` WHERE `adspaceIdMd`='".$sp."'";
$RS=mysql_query($SQL) or die("Error #val");
$ROW=mysql_fetch_array($RS) or die("Error #val2");
$sp=$ROW['adspaceId'];  // adspace id

if($bk){ // Non-campaign ads
# Inser into track
$SQL="INSERT INTO advertise_click_trac SET adsiteId=".$s.",adspaceId=".$sp.", advertiseId=".$bk.", client_IP='".$_SERVER['REMOTE_ADDR']."',click_time=NOW(),`browser_used`='".$_SERVER['HTTP_USER_AGENT']."'";
$RS=mysql_query($SQL) or die("Error #bk1");
# Update click counter
$SQL="UPDATE `advertise_details` SET `clicks_attempts`=`clicks_attempts` +1 WHERE advertiseId=".$bk."";
$RS=mysql_query($SQL) or die("Error #bk2");

// # Keep track of click for a PRTICULAR SITE
// $SQL="SELECT 	adsiteId FROM `advertise_details` WHERE advertiseId=".$bk."";
// $RS=mysql_query($SQL) or die("Error #bk11");
// $ROW=mysql_fetch_array($RS)or die("Error #bk12");
// $SQL="INSERT INTO `adsite_click_trac` SET adsiteId=".$ROW[0].", client_IP='".$_SERVER['REMOTE_ADDR']."',click_time=NOW()";
// $RS=mysql_query($SQL) or die("Error #bk13");

## Redirect info
$SQL="SELECT destinationUrl FROM advertise_master WHERE advertiseId=".$bk."";
$RS=mysql_query($SQL) or die("Error #bk3");
$ROW=mysql_fetch_array($RS) or die("Error #bk4");
if($ROW['destinationUrl']=="")$ROW['destinationUrl']=$_SERVER['HTTP_REFERER'];
if(!strstr($ROW['destinationUrl'], "http"))$ROW['destinationUrl']="http://".$ROW['destinationUrl'];
$redirect=$ROW['destinationUrl'];
#header("Location:".$ROW['destinationUrl']);
}else if($a && $g && $c) { // Campaign ads
# Inser into track
$SQL="INSERT INTO advertise_click_trac SET adsiteId=".$s.",adspaceId=".$sp.", campaign_ad='Y', adgroup_ad_id=".$a.", adgroup_id=".$g.", campaign_id=".$c.", client_IP='".$_SERVER['REMOTE_ADDR']."',click_time=NOW(),`browser_used`='".$_SERVER['HTTP_USER_AGENT']."'";
$RS=mysql_query($SQL) or die("Error #agc1");

# Update spent budget info where CPC is applicable
if($N=='C'){  // Custom network
  ## Update adgroup budget spent
  $SQL="SELECT CN.`bid`,CN.`per`,AG.`today`,CURDATE() AS CD FROM `create_network` CN, `adgroup` AG WHERE AG.`adgroup_network`=CN.`network_id` AND AG.`adgroup_id`=".$g."";
  $RS=mysql_query($SQL) or die("Error #CD1");
  $ROW=mysql_fetch_array($RS) or die("Error #CD2");
  if($ROW[per]=='CPC' && $ROW[today]!=$ROW[CD]){
  $SQL="UPDATE  `adgroup` SET `today`=CURDATE(), today_spent='".$ROW[bid]."' WHERE `adgroup_id`=".$g."";
  $RS=mysql_query($SQL) or die("Error #CD3");
  }
  else if($ROW[per]=='CPC' && $ROW[today]==$ROW[CD]){
  $SQL="UPDATE  `adgroup` SET today_spent= today_spent + ".$ROW[bid]." WHERE `adgroup_id`=".$g."";
  $RS=mysql_query($SQL) or die("Error #CD4");
  }
  
  ## Update adcampaign budget spent
  $SQL2="SELECT `today`,CURDATE() AS CD FROM `adcampaign` WHERE `campaign_id`=".$c."";
  $RS2=mysql_query($SQL2) or die("Error #CD12");
  $ROW2=mysql_fetch_array($RS2) or die("Error #CD22");
  if($ROW[per]=='CPC' && $ROW2[today]!=$ROW2[CD]){
  $SQL3="UPDATE  `adcampaign` SET `today`=CURDATE(), today_spent='".$ROW[bid]."',total_spent=total_spent +  ".$ROW[bid]." WHERE `campaign_id`=".$c."";
  $RS3=mysql_query($SQL3) or die("Error #CD33");
  }
  else if($ROW[per]=='CPC' && $ROW2[today]==$ROW2[CD]){
  $SQL4="UPDATE  `adcampaign` SET today_spent= today_spent + ".$ROW[bid].",total_spent=total_spent +  ".$ROW[bid]." WHERE `campaign_id`=".$c."";
  $RS4=mysql_query($SQL4) or die("Error #CD44");
  }
}
else if($N=='D'){ // Predefined network
  ## Update adgroup budget spent
  $SQL="SELECT ND.`networkMinPrice`,NM.`networkBidType`,AG.`today`,CURDATE() AS CD FROM `network_details` ND,`network_master` NM, `adgroup` AG WHERE NM.`networkId`=ND.`networkId` AND AG.`adgroup_network`=ND.`networkId` AND AG.`adgroup_id`=".$g."";
  $RS=mysql_query($SQL) or die("Error #CD5");
  $ROW=mysql_fetch_array($RS) or die("Error #CD6");
  if($ROW[networkBidType]=='CPC' && $ROW[today]!=$ROW[CD]){
  $SQL="UPDATE  `adgroup` SET `today`=CURDATE(), today_spent='".$ROW[networkMinPrice]."' WHERE `adgroup_id`=".$g."";
  $RS=mysql_query($SQL) or die("Error #CD7");
  }
  else if($ROW[networkBidType]=='CPC' && $ROW[today]==$ROW[CD]){
  $SQL="UPDATE  `adgroup` SET today_spent= today_spent + ".$ROW[networkMinPrice]." WHERE `adgroup_id`=".$g."";
  $RS=mysql_query($SQL) or die("Error #CD8");
  }
  
  ## Update adcampaign budget spent
  $SQL9="SELECT `today`,CURDATE() AS CD FROM `adcampaign` WHERE `campaign_id`=".$c."";
  $RS9=mysql_query($SQL9) or die("Error #CD19");
  $ROW9=mysql_fetch_array($RS9) or die("Error #CD29");
  if($ROW[networkBidType]=='CPC' && $ROW9[today]!=$ROW9[CD]){
  $SQL10="UPDATE  `adcampaign` SET `today`=CURDATE(), today_spent='".$ROW[networkMinPrice]."',total_spent=total_spent +  ".$ROW[networkMinPrice]." WHERE `campaign_id`=".$c."";
  $RS10=mysql_query($SQL10) or die("Error #CD310");
  }
  else if($ROW[networkBidType]=='CPC' && $ROW9[today]==$ROW9[CD]){
  $SQL11="UPDATE  `adcampaign` SET today_spent= today_spent + ".$ROW[networkMinPrice].",total_spent=total_spent +  ".$ROW[networkMinPrice]." WHERE `campaign_id`=".$c."";
  $RS11=mysql_query($SQL11) or die("Error #CD411");
  }
}


## Redirect info
$SQL="SELECT dest_url FROM `adgroup_ad` WHERE adgroup_ad_id=".$a."";
$RS=mysql_query($SQL) or die("Error #agc3");
$ROW=mysql_fetch_array($RS) or die("Error #agc4".mysql_error());
if($ROW['dest_url']=="")$ROW['dest_url']=$_SERVER['HTTP_REFERER'];
if(!strstr($ROW['dest_url'], "http"))$ROW['dest_url']="http://".$ROW['dest_url'];
$redirect=$ROW['dest_url'];
#header("Location:".$ROW['dest_url']);
}else{
$redirect=$_SERVER['HTTP_REFERER'];
#header("Location:".$_SERVER['HTTP_REFERER']);
}
# Inser into track for every visit
$SQL="INSERT INTO `adspace_click_trac` SET adspaceId=".$sp.",adsiteId=".$s.", client_IP='".$_SERVER['REMOTE_ADDR']."',`visit_time`=NOW()";
$RS=mysql_query($SQL) or die("Error #imp3".mysql_error());

## Redirect to the destination
header("Location:".$redirect);
ob_end_flush();

function __getPostVal($p){
$p=$_REQUEST[$p];
if(strlen($p)>8){
$p=substr($p,4);  // strip out first 4 chars
$p=substr($p,0,-4);  // stripout last 4 chars
}else $p=0;
return $p;
}

?>
